<?php

namespace App\Http\Form\KfCashRegisterV2;

use App\Http\Form\FormValidate;
use App\Support\CustomForm\Validate\ValidateSupport;
use Illuminate\Support\Facades\Validator;


class KfCashRegisterV2FormValidate extends FormValidate
{

    /**
     * @param string $bankTypeCodeField
     * @param string $bankTypeNameField
     * @param bool $required
     * @return ValidateSupport
     */
    public static function fyBankTypeValidate(
        string $bankTypeCodeField = "bankTypeCode",
        string $bankTypeNameField = "bankTypeName",
        bool $required = true
    ): ValidateSupport {
        return static::getInstance()->setRequired($required)
                     ->setDataType(ValidateSupport::DATA_TYPE_ARRAY)
                     ->setClosure(
                         function ($attribute, $value, $fail) use (
                             $bankTypeCodeField,
                             $bankTypeNameField
                         ) {
                             $validator = Validator::make(
                                 $value,
                                 [
                                     $bankTypeCodeField => "required|string|min:0|max:12",
                                     $bankTypeNameField => "required|string|min:0|max:48",
                                 ],
                                 [
                                     "string" => ":attribute 数据类型需为字符串",
                                     "int"    => ":attribute 数据类型需为字符串",
                                     "array"  => ":attribute 数据类型需为数组"
                                 ],
                                 [
                                     $bankTypeCodeField => "银行分类code",
                                     $bankTypeNameField => "银行名称",
                                 ]
                             );
                             if ($validator->fails()) {
                                 $fail($validator->errors()->first());
                             }
                         }
                     );
    }

    /**
     * @param string $provinceCodeField
     * @param string $cityCodeField
     * @param string $districtCodeField
     * @param string $addressPrefix
     * @param string $regionBankCodeField
     * @param bool $required
     * @return ValidateSupport
     */
    public static function fyAddressValidate(
        string $provinceCodeField = "provinceCode",
        string $cityCodeField = "cityCode",
        string $districtCodeField = "districtCode",
        string $addressPrefix = "addressPrefix",
        string $regionBankCodeField = "regionBankCode",
        bool $required = true
    ): ValidateSupport {
        return static::getInstance()->setRequired($required)
                     ->setDataType(ValidateSupport::DATA_TYPE_ARRAY)
                     ->setClosure(
                         function ($attribute, $value, $fail) use (
                             $provinceCodeField,
                             $cityCodeField,
                             $districtCodeField,
                             $addressPrefix,
                             $regionBankCodeField
                         ) {
                             $validator = Validator::make(
                                 $value,
                                 [
                                     $provinceCodeField   => "required|string|min:0|max:12",
                                     $cityCodeField       => "required|string|min:0|max:12",
                                     $districtCodeField   => "required|string|min:0|max:12",
                                     $addressPrefix       => "required|string|min:0|max:120",
                                     $regionBankCodeField => "required|string|min:1|max:12"
                                 ],
                                 [
                                     "string" => ":attribute 数据类型需为字符串",
                                     "int"    => ":attribute 数据类型需为字符串",
                                     "array"  => ":attribute 数据类型需为数组"
                                 ],
                                 [
                                     $provinceCodeField   => "省份code",
                                     $cityCodeField       => "市级code",
                                     $districtCodeField   => "区域code",
                                     $regionBankCodeField => "区域银行code",
                                     $addressPrefix       => "地址前缀"
                                 ]
                             );
                             if ($validator->fails()) {
                                 $fail($validator->errors()->first());
                             }
                         }
                     );
    }

    /**
     * @param string $regionBankCodeField
     * @param string $bankTypeCodeField
     * @param string $bankNoField
     * @param string $bankNameField
     * @param bool $required
     * @return ValidateSupport
     */
    public static function fyBranchBankValidate(
        string $regionBankCodeField = "regionBankCode",
        string $bankTypeCodeField = "bankTypeCode",
        string $bankNoField = "bankNo",
        string $bankNameField = "bankName",
        bool $required = true
    ): ValidateSupport {
        return static::getInstance()->setRequired($required)
                     ->setDataType(ValidateSupport::DATA_TYPE_ARRAY)
                     ->setClosure(
                         function ($attribute, $value, $fail) use (
                             $regionBankCodeField,
                             $bankTypeCodeField,
                             $bankNoField,
                             $bankNameField
                         ) {
                             $validator = Validator::make(
                                 $value,
                                 [
                                     $regionBankCodeField => "required|string|min:0|max:12",
                                     $bankTypeCodeField   => "required|string|min:0|max:12",
                                     $bankNoField         => "required|string|min:0|max:36",
                                     $bankNameField       => "required|string|min:0|max:120",
                                 ],
                                 [
                                     "string" => ":attribute 数据类型需为字符串",
                                     "int"    => ":attribute 数据类型需为字符串",
                                     "array"  => ":attribute 数据类型需为数组"
                                 ],
                                 [
                                     $regionBankCodeField => "区域银行code",
                                     $bankTypeCodeField   => "银行分类code",
                                     $bankNoField         => "支行编号code",
                                     $bankNameField       => "支行名称"
                                 ]
                             );
                             if ($validator->fails()) {
                                 $fail($validator->errors()->first());
                             }
                         }
                     );
    }
}
